//
//  LWXRticleViewController.m
//  EPA
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LWXRticleViewController.h"
#import "LWXNetWorkRequestManager.h"
#import "LWXCommentController.h"
#import "DBManager.h"
#import "UserinfoManager.h"
@interface LWXRticleViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
//属性存储 html
@property (nonatomic, strong) NSString *htmlStr;

@end

@implementation LWXRticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    self.webView.delegate = self;
    [self.view addSubview:_webView];
    [self loadData];

    
    
    // 判断是否收藏
    NSString *collectBtnTitle = nil;
    if ([[DBManager shareManager]hasCollect:self.model.ID]) {
        collectBtnTitle = @"取消收藏";
        
    }else {
        collectBtnTitle = @"收藏";
    }

    //收藏
    UIBarButtonItem *collectBtn = [[UIBarButtonItem alloc] initWithTitle:collectBtnTitle style:(UIBarButtonItemStylePlain) target:self action:@selector(collectAction:)];
    
    //评论
    UIBarButtonItem *commentBtn = [[UIBarButtonItem alloc] initWithTitle:@"评论" style:(UIBarButtonItemStylePlain) target:self action:@selector(commentAction:)];
    
    
    //分享
    UIBarButtonItem * shareBtn = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:(UIBarButtonItemStylePlain) target:self action:@selector(shareAction:)];
    
    self.navigationItem.rightBarButtonItems = @[collectBtn,commentBtn,shareBtn];

     // Do any additional setup after loading the view.
}

- (void)loadData {
    NSDictionary *paramsData = [NSDictionary dictionary];
    
   
    if ([UserinfoManager shareManager].auth ) {
        paramsData = @{@"contentid":self.model.ID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":[UserinfoManager shareManager].auth,@"version":@"3.0.2"};
    }else{
        paramsData = @{@"contentid":self.model.ID,@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"};
    }
    
    [LWXNetWorkRequestManager requestURL:[NSURL URLWithString:READCONTENT_URL] Method:LWX_POST Params:paramsData Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
              dispatch_async(dispatch_get_main_queue(), ^{
            
            [_webView loadHTMLString:jsonDict[@"data"][@"html"] baseURL:nil];
                  self.htmlStr = jsonDict[@"data"][@"html"];
        });
        
    }];


    
}


- (void)collectAction:(UIBarButtonItem *)collectBtn {
    
    if ([UserinfoManager shareManager].auth) {
         if ([collectBtn.title isEqualToString:@"收藏"]) {
        [collectBtn setTitle:@"取消收藏"];
        [[DBManager shareManager] collectReadingModel:self.model html:self.htmlStr];
             
    }else {
        [collectBtn setTitle:@"收藏"];
        [[DBManager shareManager] cancelCollectAricle:self.model.ID];
        
    }


    }else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请登录账号" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alertC addAction:alertAction];
  dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertC animated:YES completion:nil];
            });

    }
    
       
    
}
- (void)commentAction:(UIBarButtonItem *)commentBtn {
    
    LWXCommentController *commentVC = [[LWXCommentController alloc] init];
    commentVC.model = self.model;
    [self.navigationController pushViewController:commentVC animated:YES];
    
}
- (void)shareAction:(UIBarButtonItem *)shareBtn {
    
    
    
}



- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // JS 语言的脚本
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
